package com.conversationboard.controller;

import java.io.IOException;
import java.security.Principal;
import java.sql.SQLException;
import java.util.Collections;
import java.util.Date;
import java.util.List;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.xml.transform.TransformerException;

import com.conversationboard.authenticator.AuthenticationCookie;
import com.conversationboard.cache.newanswers.INewAnswersCache;
import com.conversationboard.cache.newanswers.NewAnswersThread;
import com.conversationboard.config.Configuration;
import com.conversationboard.controller.ipaddress.IPAddress;
import com.conversationboard.model.Role;
import com.conversationboard.model.User;
import com.conversationboard.site.Site;

@WebServlet(name = "SiteNewAnswersControllerServlet", urlPatterns = "/SiteNewAnswersControllerServlet")
public class SiteNewAnswersControllerServlet extends HttpServlet {

	private static final long serialVersionUID = -2695477372961466975L;


	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

		response.setCharacterEncoding("UTF-8");
		request.setCharacterEncoding("UTF-8");

		try {

			Principal principal = request.getUserPrincipal();
			String loginId = null;

			if (principal == null) {
				if (AuthenticationCookie.getUserName(request) != null) {
					response.sendRedirect(Configuration.getInstance().getRoot() + "/LoginControllerServlet?autologin=true&referrer=SiteNewAnswersControllerServlet");
					return;
				}
			}

			if (principal != null) {
				loginId = principal.getName();
			}

			User user = User.get(loginId);

			if (user.getRole() != Role.Anonymous) {
				user.setLastAccessed(new Date());
			}

			user.updateLastLoggedIn(IPAddress.getIPAddress(request));

			INewAnswersCache siteNewAnswers = Site.getSiteNewAnswers();
			
			if (siteNewAnswers.isEmpty()) {
				siteNewAnswers.populate();
			}
			
			List<NewAnswersThread> threads = siteNewAnswers.getAllThreads();

			if (request.getParameter("sort") != null) {
				Collections.sort(threads);
				request.setAttribute("sort", true);
			}

			request.setAttribute("user", user);
			request.setAttribute("threads", threads);

			if ((request.getParameter("xml") != null) && (request.getParameter("xml").equalsIgnoreCase("true"))) {

				int numberOfNewAnswers = 0;

				if (request.getParameter("number") != null) {
					numberOfNewAnswers = Integer.parseInt(request.getParameter("number"));
				}

				String stylesheet = request.getParameter("stylesheet");
				XmlNewAnswers.deliverAsXML(response, request.getContextPath(), stylesheet, numberOfNewAnswers, threads, user);

				return;
			}

			RequestDispatcher dispatcher = request.getRequestDispatcher("/Pages/sitenewanswers.jsp");

			dispatcher.forward(request, response);

		} catch (SQLException e) {
			throw new ServletException(e);
		} catch (TransformerException e) {
			throw new ServletException(e);
		} catch (ServletException e) {
			throw new ServletException(e);
		} catch (IOException e) {
			throw new ServletException(e);
		}

	}

}
